//
//  MeProfileViewController.m
//  SXH
//
//  Created by 房 国生 on 15/7/10.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "MeProfileViewController.h"
#import "VPImageCropperViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "CommonService.h"
#import "LPUtils.h"
#import "UIImageView+WebCache.h"
#import "UserEntity.h"
#import "AppDelegate.h"
#import "SRMonthPicker.h"
#import "SXHScrollView.h"
#import "FindMeViewController.h"
#import <AVFoundation/AVFoundation.h>
#define ORIGINAL_MAX_WIDTH 640.0f

@interface MeProfileViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, VPImageCropperDelegate, UITextFieldDelegate, SRMonthPickerDelegate> {
}

@property (nonatomic, strong) IBOutlet UIView *viewPopMenu;
@property (nonatomic, strong) IBOutlet SXHScrollView *scrollViewMain;
@property (nonatomic, strong) IBOutlet UIImageView *portraitImageView;
@property (nonatomic, strong) IBOutlet UITextField *textFieldNickName;
@property (nonatomic, strong) IBOutlet UITextField *textFieldDesc;
@property (nonatomic, strong) IBOutlet UITextField *textFieldSex;
@property (nonatomic, strong) IBOutlet UITextField *textFieldBirthday;
@property (nonatomic, strong) IBOutlet UITextField *textFieldZone;

@property (nonatomic, strong) UIActionSheet *choiceSheet;
@property (nonatomic, strong) UIActionSheet *choiceSexSheet;

@property (nonatomic, strong) IBOutlet UIButton *btnSave;
@property (nonatomic, strong) IBOutlet UIButton *btnLogOut;
@property (nonatomic, strong) IBOutlet UIDatePicker *datePicker;

@property (weak, nonatomic) IBOutlet SRMonthPicker *monthPicker;

@end


#define K_title     @"个人资料"

@implementation MeProfileViewController

@synthesize textFieldDesc,textFieldNickName, textFieldSex, textFieldZone, textFieldBirthday;
@synthesize meVC, scrollViewMain, choiceSexSheet, choiceSheet;
@synthesize btnSave, btnLogOut, datePicker;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = K_title;
    if (is_IOS_7) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [btnSave setTitle:@"发表" forState:UIControlStateNormal];
    //定义按钮标题字体格式
    //[btnSave.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
    [btnSave setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btnSave addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self initViewController];
    
    [self initImageViewHeader];
    
    textFieldDesc.delegate = self;
    textFieldNickName.delegate = self;
    textFieldSex.delegate = self;
    textFieldZone.delegate = self;
    textFieldBirthday.delegate = self;
    
    [btnLogOut.layer setMasksToBounds:YES];
    [btnLogOut.layer setCornerRadius:5.0]; //设置矩形四个圆角半径
    
    self.monthPicker.monthPickerDelegate = self;
    // Some options to play around with
    self.monthPicker.maximumYear = @2020;
    self.monthPicker.minimumYear = @1900;
    self.monthPicker.yearFirst = YES;
    
    [self.viewPopMenu setFrame:CGRectMake(0, ScreenHeight,ScreenWidth, _viewPopMenu.frame.size.height)];
    [self.view addSubview:self.viewPopMenu];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (IBAction)saveAction:(id)sender {
    
    if ([btnSave.titleLabel.text isEqualToString:@"编辑"] == YES) {
        [self enableTextFieldController:YES];
        [btnSave setTitle:@"完成" forState:UIControlStateNormal];
        return;
    }
    UserEntity *userEntity = [UserEntity sharedInstance];
    [self uploadHeader:userEntity];

}

- (void)saveUserInfo {
    UserEntity *userEntity = [UserEntity sharedInstance];
    //sex：性别（0，保密   1，男  2，女）
    NSString *sex = @"0";
    if ([textFieldSex.text isEqualToString:@"保密"] == YES) {
        sex = @"0";
    } else if ([textFieldSex.text isEqualToString:@"男"] == YES) {
        sex = @"1";
    } else {
        sex = @"2";
    }
    CommonService *service = [[CommonService alloc] init];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           userEntity.userId, @"id",
                           textFieldNickName.text, @"nickname",
                           textFieldBirthday.text, @"birthday",
                           textFieldZone.text, @"cityname",
                           sex, @"sex",
                           textFieldDesc.text, @"signature",nil];
    [service getNetWorkData:param
                       Path:USER_EDIT
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *)entity;
                      DebugLog(@"%@", dic);
                      //code	返回码 0 更新成功 2 参数错误 -330 用户ID不能为空  -331 更新失败
                      NSNumber *code = [dic objectForKey:@"code"];
                      if ([code intValue] == 0) {
                          ALERT_ERR_MSG(@"资料更新成功");
                          NSDictionary *dicUser = [dic objectForKey:@"row"];
                          UserEntity *userEntity = [UserEntity sharedInstance];
                          UserEntity *userTempEntity = [[UserEntity alloc] init];
                          userTempEntity = [userTempEntity initWithAttributes:dicUser];
                          [userEntity deepCopy:userTempEntity];
                          userEntity.login = YES;
                          
                          [meVC getFineMeData];
                          //保存用户信息
                          [[AppDelegate instance] saveUserEntity:userEntity];
                          //
                      } else {
                          ALERT_ERR_MSG(@"资料更新失败");
                      }
                      [super showLoadingView:NO];
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      DebugLog(@"%@", message);
                      [super showLoadingView:NO];
                  }];
}

- (void)uploadHeader:(UserEntity *)userEntity {
    [super showLoadingView:YES];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSString *fileName = [NSString stringWithFormat:@"img_%@",[LPUtils NSDateToNSString:[NSDate date]]];
        NSDictionary *dicRes = [CommonService uploadUserImage:userEntity FileName:fileName
                                                        Image:UIImageJPEGRepresentation(_portraitImageView.image,0.5)];
        if (dicRes == nil) {
            [super showLoadingView:NO];
            ALERT_ERR_MSG(@"上传资料失败");
        } else {
            NSNumber *code = [dicRes objectForKey:@"code"];
            dispatch_async(dispatch_get_main_queue(), ^{
                if ([code intValue] == 0) {
                    [self saveUserInfo];
                } else {
                    ALERT_ERR_MSG(@"上传资料失败");
                    [super showLoadingView:NO];
                }
            });
        }
    });
}

- (IBAction)loginOut:(id)sender {
    [[AppDelegate instance] removeUserEntity];
    UserEntity *entity = [UserEntity sharedInstance];
    [entity cleanData];
    FindMeViewController * mvc = (FindMeViewController*)meVC;
    mvc.userId = nil;
    [mvc judgeIsFromOtherVC];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loadPortrait {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
        NSURL *portraitUrl = [NSURL URLWithString:Photo_Path];
        UIImage *protraitImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:portraitUrl]];
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.portraitImageView.image = protraitImg;
        });
    });
}

- (void)editPortrait {
    [self.view endEditing:YES];
    choiceSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"拍照", @"从相册中选取", nil];
    [choiceSheet showInView:self.view];
}

#pragma mark VPImageCropperDelegate
- (void)imageCropper:(VPImageCropperViewController *)cropperViewController didFinished:(UIImage *)editedImage {
    self.portraitImageView.image = editedImage;
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
        // TO DO
    }];
}

- (void)imageCropperDidCancel:(VPImageCropperViewController *)cropperViewController {
    [cropperViewController dismissViewControllerAnimated:YES completion:^{
    }];
}

#pragma mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (actionSheet == choiceSheet) {
        if (buttonIndex == 0) {
            // 拍照
            NSString *mediaType = AVMediaTypeVideo;
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
            if(authStatus == AVAuthorizationStatusDenied || authStatus == AVAuthorizationStatusRestricted ) {
                UIAlertController *controller = [UIAlertController alertControllerWithTitle:nil message:@"请前往设置打开相机权限" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [controller dismissViewControllerAnimated:YES completion:nil];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=CAMERA"]];
                }];
                UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [controller dismissViewControllerAnimated:YES completion:nil];
                }];
                [controller addAction:confirmAction];
                [controller addAction:cancelAction];
                [self presentViewController:controller animated:YES completion:nil];
                return;
            }
            if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos]) {
                UIImagePickerController *controller = [[UIImagePickerController alloc] init];
                controller.sourceType = UIImagePickerControllerSourceTypeCamera;
                if ([self isFrontCameraAvailable]) {
                    controller.cameraDevice = UIImagePickerControllerCameraDeviceFront;
                }
                NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
                [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
                controller.mediaTypes = mediaTypes;
                controller.delegate = self;
                [self presentViewController:controller
                                   animated:YES
                                 completion:^(void){
                                     NSLog(@"Picker View Controller is presented");
                                 }];
            }
            
        } else if (buttonIndex == 1) {
            // 从相册中选取
            if ([self isPhotoLibraryAvailable]) {
                UIImagePickerController *controller = [[UIImagePickerController alloc] init];
                controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
                [mediaTypes addObject:(__bridge NSString *)kUTTypeImage];
                controller.mediaTypes = mediaTypes;
                controller.delegate = self;
                [self presentViewController:controller
                                   animated:YES
                                 completion:^(void){
                                     NSLog(@"Picker View Controller is presented");
                                 }];
            }
        }
    } else {
        if (buttonIndex == 0) {
            textFieldSex.text = @"保密";
        } else if (buttonIndex == 1) {
            textFieldSex.text = @"男";
        } else if (buttonIndex == 2) {
            textFieldSex.text = @"女";
        }
    }
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:^() {
        UIImage *portraitImg = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        portraitImg = [self imageByScalingToMaxSize:portraitImg];
        // 裁剪
        VPImageCropperViewController *imgEditorVC = [[VPImageCropperViewController alloc] initWithImage:portraitImg cropFrame:CGRectMake(0, 100.0f, self.view.frame.size.width, self.view.frame.size.width) limitScaleRatio:3.0];
        imgEditorVC.delegate = self;
        [self presentViewController:imgEditorVC animated:YES completion:^{
            // TO DO
        }];
    }];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^(){
    }];
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
}

#pragma mark camera utility
- (BOOL) isCameraAvailable{
    return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL) isRearCameraAvailable{
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}

- (BOOL) isFrontCameraAvailable {
    return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}

- (BOOL) doesCameraSupportTakingPhotos {
    return [self cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypeCamera];
}

- (BOOL) isPhotoLibraryAvailable{
    return [UIImagePickerController isSourceTypeAvailable:
            UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) canUserPickVideosFromPhotoLibrary{
    return [self
            cameraSupportsMedia:(__bridge NSString *)kUTTypeMovie sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (BOOL) canUserPickPhotosFromPhotoLibrary{
    return [self
            cameraSupportsMedia:(__bridge NSString *)kUTTypeImage sourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

- (BOOL) cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType{
    __block BOOL result = NO;
    if ([paramMediaType length] == 0) {
        return NO;
    }
    NSArray *availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:paramSourceType];
    [availableMediaTypes enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *mediaType = (NSString *)obj;
        if ([mediaType isEqualToString:paramMediaType]){
            result = YES;
            *stop= YES;
        }
    }];
    return result;
}

#pragma mark image scale utility
- (UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage {
    if (sourceImage.size.width < ORIGINAL_MAX_WIDTH) return sourceImage;
    CGFloat btWidth = 0.0f;
    CGFloat btHeight = 0.0f;
    if (sourceImage.size.width > sourceImage.size.height) {
        btHeight = ORIGINAL_MAX_WIDTH;
        btWidth = sourceImage.size.width * (ORIGINAL_MAX_WIDTH / sourceImage.size.height);
    } else {
        btWidth = ORIGINAL_MAX_WIDTH;
        btHeight = sourceImage.size.height * (ORIGINAL_MAX_WIDTH / sourceImage.size.width);
    }
    CGSize targetSize = CGSizeMake(btWidth, btHeight);
    return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];
}

- (UIImage *)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize {
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth  = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else
            if (widthFactor < heightFactor)
            {
                thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
            }
    }
    UIGraphicsBeginImageContext(targetSize); // this will crop
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width  = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil) NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}

- (void) initImageViewHeader {
    
    [_portraitImageView.layer setCornerRadius:(_portraitImageView.frame.size.height/2)];
    [_portraitImageView.layer setMasksToBounds:YES];
    [_portraitImageView setContentMode:UIViewContentModeScaleAspectFill];
    [_portraitImageView setClipsToBounds:YES];
    _portraitImageView.layer.shadowColor = [UIColor blackColor].CGColor;
    _portraitImageView.layer.shadowOffset = CGSizeMake(4, 4);
    _portraitImageView.layer.shadowOpacity = 0.5;
    _portraitImageView.layer.shadowRadius = 2.0;
    //_portraitImageView.layer.borderColor = [[UIColor blackColor] CGColor];
    //_portraitImageView.layer.borderWidth = 2.0f;
    _portraitImageView.userInteractionEnabled = YES;
    _portraitImageView.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *portraitTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editPortrait)];
    [_portraitImageView addGestureRecognizer:portraitTap];
}

- (void) enableTextFieldController:(BOOL) enable {
    
    textFieldDesc.enabled = enable;
    textFieldNickName.enabled = enable;
    textFieldSex.enabled = enable;
    textFieldZone.enabled = enable;
    textFieldBirthday.enabled = enable;
}

- (void) initViewController {
    
    UserEntity *userEntity = [UserEntity sharedInstance];
    if (userEntity.login == YES) {
        textFieldDesc.text = userEntity.signature;
        textFieldNickName.text = userEntity.nickName;
        
        if ([userEntity.sex intValue] == 0) {
            textFieldSex.text = @"保密";
        } else if ([userEntity.sex intValue] == 1) {
            textFieldSex.text = @"男";
        } else if ([userEntity.sex intValue] == 2) {
            textFieldSex.text = @"女";
        }
        
        textFieldBirthday.text = userEntity.birthday;
        textFieldZone.text = userEntity.cityName;
        [_portraitImageView sd_setImageWithURL:[NSURL URLWithString:userEntity.userAlbum]];
        
        [btnSave setTitle:@"编辑" forState:UIControlStateNormal];
        [self enableTextFieldController:NO];
        btnLogOut.hidden = NO;
    } else {
        [btnSave setTitle:@"完成" forState:UIControlStateNormal];
        btnLogOut.hidden = YES;
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    if (textField == self.textFieldBirthday) {
        [scrollViewMain setContentOffset:CGPointMake(0, 40) animated:YES];
        [self popSpotMenu];
        return NO;
    } else if (textField == self.textFieldZone) {
        [scrollViewMain setContentOffset:CGPointMake(0, 60) animated:YES];
        [self performSelector:@selector(hidenPopMenu) withObject:nil afterDelay:0.0f];

    } else if (textField == self.textFieldDesc) {
        [scrollViewMain setContentOffset:CGPointMake(0, 90) animated:YES];
        [self performSelector:@selector(hidenPopMenu) withObject:nil afterDelay:0.0f];

    } else if (textField == self.textFieldSex) {
        
        choiceSexSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                  delegate:self
                                         cancelButtonTitle:@"取消"
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:@"保密", @"男", @"女",nil];
        [choiceSexSheet showInView:self.view];
        [self performSelector:@selector(hidenPopMenu) withObject:nil afterDelay:0.0f];
        [self.view endEditing:YES];
        return NO;
    }
    else if (textField == textFieldNickName)
    {
        [self performSelector:@selector(hidenPopMenu) withObject:nil afterDelay:0.0f];
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    if (textField == self.textFieldNickName) {
        [textFieldSex becomeFirstResponder];
    } else if (textField == self.textFieldSex) {
//        [textFieldBirthday becomeFirstResponder];
    } else if (textField == self.textFieldBirthday) {
//        [textFieldZone becomeFirstResponder];
    } else if (textField == self.textFieldZone) {
        [textFieldDesc becomeFirstResponder];
    } else if (textField == self.textFieldDesc) {
        [textFieldDesc resignFirstResponder];
        [scrollViewMain setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    return YES;
}

- (NSString*)formatDate:(NSDate *)date
{
    // A convenience method that formats the date in Month-Year format
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"y-mm";
    return [formatter stringFromDate:date];
}

- (void)monthPickerWillChangeDate:(SRMonthPicker *)monthPicker
{
    // Show the date is changing (with a 1 second wait mimicked)
    //self.textFieldBirthday.text = [NSString stringWithFormat:@"%@", [self formatDate:monthPicker.date]];
}

- (void)monthPickerDidChangeDate:(SRMonthPicker *)monthPicker
{
    // All this GCD stuff is here so that the label change on -[self monthPickerWillChangeDate] will be visible
    dispatch_queue_t delayQueue = dispatch_queue_create("com.simonrice.SRMonthPickerExample.DelayQueue", 0);
    
    dispatch_async(delayQueue, ^{
        // Wait 1 second
        sleep(1);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.textFieldBirthday.text = [NSString stringWithFormat:@"%@", [self formatDate:self.monthPicker.date]];
        });
    });
}

-(IBAction)dateChanged:(id)sender{
    UIDatePicker* control = (UIDatePicker*)sender;
    NSDate* _date = control.date;
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *strDate = [formatter stringFromDate:_date];
    self.textFieldBirthday.text = strDate;
    /*添加你自己响应代码*/
}

- (IBAction)doSelect:(id)sender {
    [self.view endEditing:YES];
    NSDate* _date = datePicker.date;
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSString *strDate = [formatter stringFromDate:_date];
    self.textFieldBirthday.text = strDate;
    
    [self hidenPopMenu];
}

- (void)hidenPopMenu {
    
    [UIView animateWithDuration:.35 animations:^{
        [self.viewPopMenu setFrame:CGRectMake(0, ScreenHeight,ScreenWidth, _viewPopMenu.frame.size.height)];
        self.viewPopMenu.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            //[self.viewListSpot removeFromSuperview];
        }
    }];
}

- (void)popSpotMenu {
    [self.view endEditing:YES];
    [UIView animateWithDuration:.35 animations:^{
        //self.viewPopMenu.backgroundColor = RGBACOLOR(0, 0, 0, .9);
        self.viewPopMenu.alpha = 1.0;
        [UIView animateWithDuration:.25 animations:^{
            [self.viewPopMenu setFrame:CGRectMake(self.viewPopMenu.frame.origin.x, ScreenHeight-self.viewPopMenu.frame.size.height, self.viewPopMenu.frame.size.width, self.viewPopMenu.frame.size.height)];
        }];
    } completion:^(BOOL finished) {
        
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"MeProfileViewController"];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"MeProfileViewController"];
}

@end